import time
from django.core.paginator import Paginator
from django.shortcuts import render, redirect

from apps.systemctl import auth, menu, vm, host_machine_ret
from .models import Vm, Vm_hm


@auth
def list_vms(request):
    error_msg = ""
    host_machine_all = host_machine_ret(request)

    menus = menu(request)
    user = request.GET.get('user')
    list_str = request.GET.get('list')
    host_machine_id = request.GET.get('product_id')
    if host_machine_id == '' or host_machine_id is None:
        host_machine_id = 0
        vms_ret = vm(request)
    else:
        vms_ids = Vm_hm.objects.filter(hmid=host_machine_id)
        vms_ret = Vm.objects.none()
        for i in vms_ids:
            git_data = Vm.objects.filter(id=i.vmid)
            vms_ret = vms_ret | git_data
    page = Paginator(vms_ret.order_by('name'), 10)
    page_id = request.GET.get('page_id')
    if page_id is None:
        page_id = 1
    stus = page.page(int(page_id))
    return render(request, 'git/gitlist.html',
                  {'ret': stus, 'error_msg': error_msg, 'pageid': int(page_id), 'page': page, 'menu': menus,
                   'list': int(list_str), 'user': user, 'product_all': host_machine_all, 'product_id': int(host_machine_id)})


@auth
def add_vms(request):
    error_msg = ''
    menus = menu(request)
    products = host_machine_ret(request)
    user = request.GET.get('user')
    list_str = request.GET.get('list')
    if request.method == 'POST':
        press_name = request.POST.get('name')
        ret = Vm.objects.filter(name=press_name)
        if ret:
            error_msg = '仓库名称已存在'
            # 取用户数据
            # 写入数据库
        else:
            press_name = request.POST.get('name')
            press_type = request.POST.get('type')
            press_host = request.POST.get('host')
            press_create_date = time.strftime("%Y-%m-%d", time.localtime())
            press_remark = request.POST.get('remark')
            press_host_machine_id = request.POST.get('product')
            press_username = request.POST.get('username')
            press_pwd = request.POST.get('pwd')
            press_cpu = request.POST.get('cpu')
            press_memory = request.POST.get('memory')
            press_disk = request.POST.get('disk')
            press_system = request.POST.get('system')

            if press_host_machine_id is not None and press_host_machine_id != '':
                # 加数据 git表和关联表
                Vm.objects.create(name=press_name,
                                  outer_ip=press_type,
                                  inner_ip=press_host,
                                  create_date=press_create_date,
                                  remark=press_remark,
                                  username=press_username,
                                  pwd=press_pwd,
                                  cpu=press_cpu,
                                  memory=press_memory,
                                  disk=press_disk,
                                  system=press_system)
                git_result = Vm.objects.get(name=press_name)
                Vm_hm.objects.create(vmid=git_result.id, hmid=press_host_machine_id, remark=press_remark)
                return redirect('/git/list?list=' + list_str + '&user=' + user)  # 验证成功跳转
            else:
                error_msg = '请选择宿主机！'

    return render(request, 'git/add_git.html',
                  {'products': products, 'error_msg': error_msg, 'menu': menus, 'list': int(list_str), 'user': user})


@auth
def edit_vms(request):
    error_msg = ''
    menus = menu(request)
    user = request.GET.get('user')
    list_str = request.GET.get('list')
    vm_id = request.GET.get('id')
    products = host_machine_ret(request)
    git_product = Vm_hm.objects.get(vmid=vm_id)
    vm_data = Vm.objects.get(id=vm_id)
    if request.method == 'POST':
        press_name = request.POST.get('name')
        press_type = request.POST.get('type')
        press_host = request.POST.get('host')
        press_remark = request.POST.get('remark')
        press_username = request.POST.get('username')
        press_pwd = request.POST.get('pwd')
        press_cpu = request.POST.get('cpu')
        press_memory = request.POST.get('memory')
        press_disk = request.POST.get('disk')
        press_system = request.POST.get('system')
        press_product_id = request.POST.get('product')
        vm_data.name = press_name
        vm_data.outer_ip = press_type
        vm_data.inner_ip = press_host
        vm_data.username = press_username
        vm_data.pwd = press_pwd
        vm_data.cpu = press_cpu
        vm_data.memory = press_memory
        vm_data.disk = press_disk
        vm_data.system = press_system
        vm_data.remark = press_remark
        git_product.hmid = press_product_id
        vm_data.save()
        git_product.save()
        return redirect('/git/list?list=' + list_str + '&user=' + user)  # 验证成功跳转
    return render(request, 'git/edit_git.html',
                  {'hmid': int(git_product.hmid), 'gitdata': vm_data, 'products': products, 'error_msg': error_msg, 'menu': menus, 'list': int(list_str), 'user': user,
                   'id': vm_id})
